package com.omnisyr.textrebuilder.util.structure;

import com.omnisyr.textrebuilder.common.Color;

public class ColorList {

	private int[][][] contents = new int[256][256][256];

	public void add(Color color) {

		this.contents[color.toSingle(0)][color.toSingle(1)][color.toSingle(2)]++;

	}

	public void remove(int r, int g, int b) {

		this.contents[r][g][b] = 0;

	}

	public boolean contains(Color color) {

		return this.contents[color.toSingle(0)][color.toSingle(1)][color.toSingle(2)] > 0;

	}

	/**
	 * 
	 * @param x
	 * @param y
	 * @param z
	 * @param halfRange
	 * @param mode      </BR>
	 *                  true for average pooling </BR>
	 *                  false for max pooling </BR>
	 * 
	 * @return
	 */
	public int count(int x, int y, int z, int halfRange, boolean mode) {

		int result = 0;

		for (int r = 0 - halfRange; r <= halfRange; r++) {

			for (int g = 0 - halfRange; g <= halfRange; g++) {

				for (int b = 0 - halfRange; b <= halfRange; b++) {

					if (mode) {

						result += this.contents[this.ensureInRange(x, r)][this.ensureInRange(y, g)][this
								.ensureInRange(z, b)];

						continue;

					}

					int temp = this.contents[this.ensureInRange(x, r)][this.ensureInRange(y, g)][this.ensureInRange(z,
							b)];

					if (result < temp) {

						result = temp;

					}

				}

			}

		}

		return result;

	}

	private int ensureInRange(int num, int off) {

		int result = num + off;

		if (result < 0) {

			return 0;

		}

		if (result > 255) {

			return 255;

		}

		return result;

	}

}
